<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script src="{{ getAssets }}js/jquery.table2excel.min.js?v={{ assets_version }}"></script>
<script>
var tablesid;
var stocks;
var isLoading = false;
var columns_10 = [
    {"bSortable": false,render:function(data, type, full, meta) {
         return productGroupName(stocks.Products);
    }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Inventory.Snd.sph;
    }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Inventory.Snd.cyl;
    }}
];

var columns_20= [
    {"bSortable": false,render:function(data, type, full, meta) {
            return productGroupName(stocks.Products);
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Inventory.Snd.sph;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Inventory.Snd.cyl;
    }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.lot_number;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return timetransDate(full.manufacture_date);
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return timetransDate(full.expiry_date);
        }}
];

var columns_30 = [
    {"bSortable": false,render:function(data, type, full, meta) {
            return productGroupName(stocks.Products);
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Inventory.Snd.model;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Inventory.Snd.color;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Inventory.Snd.size;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Inventory.Snd.genre;
        }}
];

var columns_40 = [
    {"bSortable": false,render:function(data, type, full, meta) {
            return productGroupName(stocks.Products);
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Inventory.Snd.sph;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Inventory.Snd.add;
        }}
];

var columns_41 = [
    {"bSortable": false,render:function(data, type, full, meta) {
            return productGroupName(stocks.Products);
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Inventory.Snd.model;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Inventory.Snd.color;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Inventory.Snd.size;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Inventory.Snd.genre;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Inventory.Snd.film_color;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.Inventory.Snd.function_info;
        }}
];

var columns_50 = [
    {"bSortable": false,render:function(data, type, full, meta) {
            return productGroupName(stocks.Products);
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return full.lot_number;
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return timetransDate(full.manufacture_date);
        }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
            return timetransDate(full.expiry_date);
        }}
];

var columns_60 = [
    {"bSortable": false,render:function(data, type, full, meta) {
            return productGroupName(stocks.Products);
        }}
];
var columns = {10 : columns_10,20 : columns_20,30 : columns_30,40 : columns_40,41 : columns_41,50 : columns_50,60 : columns_60} ;
var defcolumns = [
    {"bSortable": false,render:function(data, type, full, meta) {
          return full.PaperNumber;
    }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
          var html = full.RealdiskNumber;
            html += '<input name="stocks_data_id" type="hidden" value="'+ full.Sid +'">';
            html += '<input name="profitloss_number" type="hidden" value="'+ full.ProfitlossNumber +'">';
            html += '<input name="inventory_id" type="hidden" value="'+ full.Inventory.Id +'">';
          return html;
    }}
    ,{"bSortable": false,render:function(data, type, full, meta) {
          return full.ProfitlossNumber;
    }}
];

$(function(){
    $.post('', {}, function (res) {
        if(res.code == 0) {
            stocks = res.data.stocks;
            tablesid = $('#dataTables-'+stocks.Products.TypeId);
            var tableColumns = columns[stocks.Products.TypeId];

            for(i=0;i<defcolumns.length;i++) {
                tableColumns.push(defcolumns[i]);
            }
            tablesid.DataTable({
                "sAjaxSource": ""
                , "bLengthChange": false
                , "columns": tableColumns
                , "serverSide": false
                , "bPaginate": false
                , "bFilter": false	//是否启动过滤、搜索功能
                , "bInfo": false //是否显示页脚信息，DataTables插件左下角显示记录数
                , "bProcessing": true //DataTables载入数据时，是否显示‘进度’提示
            });

            $.each(res.data.stocks_data, function(index, value){
                tablesid.DataTable().row.add(value).draw();
            });
            tablesid.show();
            if(stocks.Auditing == 1) {
                $('.submit-order').hide();
            } else {
                $('.submit-order').show();
            }
            isLoading = true
        }
    }, 'json');

    $('.submit-order').click(function () {
        var _data  = {
            stocks_id:stocks.Id,
            datatable:[],
        };
        tablesid.find("tbody tr").each(function(){
            var data={};
            if($(this).find("input[name='stocks_data_id']").length > 0) {
                data.stocks_id=Number($(this).find("input[name='stocks_data_id']").val());
                data.profitloss_number=Number($(this).find("input[name='profitloss_number']").val());
                data.inventory_id=Number($(this).find("input[name='inventory_id']").val());
                _data.datatable.push(data);
            }
        });
        if(_data.datatable.length<=0){
            layer.confirm("当前盘点未产生损益信息,是否结束盘点？", function(c) {
                layer.close(c);
                _data.datatable = JSON.stringify(_data.datatable);
                $.post('{{ urlfor "StocksController.SaveProfitLoss" }}', _data, function (res) {
                    if(res.code == 1) {
                        layer.alert(res.msg);return false;
                    }
                    layer.alert('盘点完成', function () {
                        location = '{{ urlfor "StocksController.Records" }}';
                    });
                });
            });
        }else{
            layer.confirm("确认损益信息无误？", function(c) {
                layer.close(c);
                _data.datatable = JSON.stringify(_data.datatable);
                $.post('{{ urlfor "StocksController.SaveProfitLoss" }}', _data, function (res) {
                    if(res.code == 1) {
                        layer.alert(res.msg);return false;
                    }
                    layer.alert('盘点完成', function () {
                        location = '{{ urlfor "StocksController.Records" }}';
                    });
                });
            });
        }

    });
});

$('#download').bind("click", function(){
    if(isLoading) {
        tablesid.table2excel({

            exclude: ".noExl",

            name: "导出损益明细",

            filename: "损益明细",

            fileext: ".xls",

            exclude_img: false,

            exclude_links: false,

            exclude_inputs: false
        });
    } else {
        $("#searchsubmit").click();
        layer.msg("还没加载结束请耐心等待一下！");return false;
    }
    return false;
});

</script>